package com.wangdong.mijiqiqi.utils;

import android.os.Environment;

import java.io.File;

/**
 * Created by liusaisai on 2016/7/14.
 * Email: liusaisai@cig.com.cn
 */
public class ApplicationStore {
    private static String m_sCacheRootFilePath = null;

    private static String m_sPersistentRootFilePath = null;

    // SD卡的内部子目录
    private static final String SD_ANDROID_DATA_DIR = "Android/data";

    // 非SD卡，手机内部子目录
    private static final String INTERNAL_DATA_DIR = "data";

    // WCM的子目录
    private static final String WCM_DATA_DIR = "cig/pcmsdata";

    // 项目包的存放目录
    private static String m_sPackage = "com.cig.pcms.nissan";

    // 缓冲文件存储目录，该目录下的文件可能被定时删除
    private static final String CACHE_DIR = "cache";

    // 持久文件存储目录，该目录下的文件不会被定时删除
    private static final String PERSISTENT_DIR = "persistent";

    /**
     * @param sPackageName
     */
    public static void init(String sPackageName) {
        m_sPackage = sPackageName;
    }

    /**
     * 获取工程目录的路径
     *
     * @return 工程路径为：<br />
     * /mnt/sdcard/Android/data/<package Name> 或者 /data/data/<package
     * Name>
     */
    private static String getProjectFilePath() {
        String sRoot = null;

        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {// SD卡（外部存储卡）可用，优先选择SD卡
            // sd卡路径为：/mnt/sdcard/Android/data
            sRoot = StringHelper.setStrEndWith(Environment
                            .getExternalStorageDirectory().getAbsolutePath(),
                    File.separator)
                    + SD_ANDROID_DATA_DIR;
        } else { // SD卡不可用，文件存储在内部存储目录: /data/data
            sRoot = StringHelper.setStrEndWith(Environment.getDataDirectory()
                    .getAbsolutePath(), File.separator)
                    + INTERNAL_DATA_DIR;
        }

        return sRoot + File.separator + m_sPackage + File.separator;
    }

    /**
     * sd卡目录为：/mnt/sdcard/Android/data/<package Name>/trs/pcmsdata
     * 内部存储的目录：/data/data/<package Name>/trs/pcmsdata
     *
     * @return
     */
    private static String getRootFilePath() {

        String sProjectFilePath = getProjectFilePath();

        return sProjectFilePath + WCM_DATA_DIR + File.separator;
    }

    /**
     * 获取缓存的根路径
     *
     * @return
     */
    public static String getCacheRootFilePath() {

        if (m_sCacheRootFilePath == null) {
            m_sCacheRootFilePath = getRootFilePath() + CACHE_DIR
                    + File.separator;
        }

        return m_sCacheRootFilePath;
    }

    /**
     * 获取持久化的根路径
     *
     * @return
     */
    public static String getPersisentRootFilePath() {

        if (m_sPersistentRootFilePath == null) {
            m_sPersistentRootFilePath = getRootFilePath() + PERSISTENT_DIR
                    + File.separator;
        }

        return m_sPersistentRootFilePath;
    }

    /**
     * 获取指定URL在缓存目录下的存放路径
     *
     * @param sURL
     * @return
     */
    public static String getCacheFilePathFromURL(String sURL) {
        // 去掉站点的http前缀
        sURL = sURL.replaceFirst("^http[s]?://", "");

        return getCacheRootFilePath() + sURL;
    }

    /**
     * 获取指定URL在持久化目录下的存放路径
     *
     * @param sURL
     * @return
     */
    public static String getPesisitentFilePathFromURL(String sURL) {
        // 去掉站点的http前缀
        sURL = sURL.replaceFirst("^http[s]?://[^/]+/", "");

        return getPersisentRootFilePath() + sURL;
    }
}
